<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="./css/reset.css" />
    <link rel="stylesheet" href="./css/iconfont.css" />
    <link rel="stylesheet" href="./css/main.css" />
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>

    <!-- 引入分页导航js文件 -->
    <script src="./libs/jquery.twbsPagination.js"></script>
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">
        文章列表
      </div>
      <div class="container-fluid common_con">
        <div class="row opt_btns">
          <div class="col-xs-6">
            <form class="form-inline">
              <select id="selCategory" name="" class="form-control input-sm">
                <option value="">所有分类</option>
                <option>未分类</option>
                <option>奇趣事</option>
                <option>会生活</option>
                <option>爱旅行</option>
              </select>
              <select id="selStatus" name="" class="form-control input-sm">
                <option value="">所有状态</option>
                <option value="草稿">草稿</option>
                <option value="已发布">已发布</option>
              </select>
              <button id="btnSearch" class="btn btn-default btn-sm">
                筛选
              </button>
            </form>
          </div>
          <div class="col-xs-6">
            <a
              href="article_release.html"
              class="btn btn-success btn-sm pull-right"
              id="release_btn"
              >发表文章</a
            >
          </div>
        </div>
        <table class="table table-striped table-bordered table-hover mp20">
          <thead>
            <tr>
              <th>标题</th>
              <th>作者</th>
              <th>分类</th>
              <th class="text-center">发表时间</th>
              <th class="text-center">状态</th>
              <th class="text-center" width="100">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>王积千造统最头</td>
              <td>杰克</td>
              <td>奇趣事</td>
              <td class="text-center">2017-06-08 07:08:46</td>
              <td class="text-center">已发布</td>
              <td class="text-center">
                <a href="article_edit.html" class="btn btn-default btn-xs"
                  >编辑</a
                >
                <a
                  href="javascript:void(0);"
                  class="btn btn-danger btn-xs delete"
                  >删除</a
                >
              </td>
            </tr>
          </tbody>
        </table>
        <div class="row text-center">
          <!-- 6.1 分页导航结构 -->
          <ul id="pagination" class="pagination-sm"></ul>
        </div>
      </div>
    </div>

    <!-- 6.1 引入模态框结构 -->
    <div class="modal fade" tabindex="-1" role="dialog" id="myModal">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">温馨提示</h4>
          </div>
          <div class="modal-body">
            <p>One fine body&hellip;</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              确认
            </button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
  </body>
</html>
<!-- 6.2 引入模态框js文件 -->
<script src="./libs/bootstrap/js/bootstrap.min.js"></script>

<!-- 1. 引入地址js文件 -->
<script src="./libs/https.js"></script>

<!-- 2. 导入模板引擎结构 -->
<script type="text/html" id="tpl-list">
  {{each}}
  <tr>
    <td>{{$value.title}}</td>
    <td>{{$value.author}}</td>
    <td>{{$value.category}}</td>
    <td class="text-center">{{$value.date}}</td>
    <td class="text-center">{{$value.state}}</td>
    <td class="text-center">
      <a href="article_edit.html?id={{$value.id}}" class="btn btn-default btn-xs" dataid="{{$value.id}}"
        >编辑</a
      >
      <a
        href="javascript:void(0);"
        class="btn btn-danger btn-xs delete" dataid="{{$value.id}}"
        >删除</a
      >
    </td>
  </tr>
  {{/each}}
</script>

<!-- 3. 导入分类模板引擎 -->
<script type="text/html" id="tpl-last">
    <option value="">所有分类</option>
  {{each}}
  <option value={{$value.id}}>{{$value.name}}</option>
  {{/each}}
</script>
<script>
  $(function () {
    // 2. 封装函数 发送Ajax请求 渲染页面
    let selPage = 1
    function list (page) {
      $.ajax({
        type: 'get',
        url: BigNew.article_query,
        data: {
          type: $('#selCategory').val(),
          state: $('#selStatus').val(),
          page: page,
          perpage: 10
        },
        success: function (backData) {
          // console.log(backData)
          // 4. 模板引擎渲染页面
          if (backData.code == 200) {
            let data = backData.data.data
            let htmlStr = template('tpl-list', data)
            $('tbody').html(htmlStr)

            // 6.4 当数据一条也没有的时候
            if (backData.data.totalCount == 0) {
              $('#pagination').html('当前没有数据...')
            } else {
              // 6. 分页导航
              $('#pagination').twbsPagination('destroy')
              $('#pagination').twbsPagination({
                totalPages: backData.data.totalPage,
                visiblePages: 10,
                startPage: page,
                initiateStartPageClick: false,
                first: '首页',
                last: '尾页',
                next: '下一页',
                prev: '上一页',
                onPageClick: function (e, page) {
                  selPage = page
                  list(page)
                }
              })
            }
          }
        }
      })
    }

    // 4. 分类列表渲染
    $.ajax({
      type: 'get',
      url: BigNew.category_list,
      success: function (backData) {
        // console.log(backData)
        if (backData.code == 200) {
          let data = backData.data
          let htmlStr = template('tpl-last', data)
          $('#selCategory').html(htmlStr)
        }
      }
    })

    // 5. 点击筛选按钮 重新渲染页面数据
    $('#btnSearch').on('click', function (e) {
      e.preventDefault()
      list(1)
      // 5.1 获取到筛选数据
      // let type = $('#selCategory').val()
      // let state = $('#selStatus').val()
      // console.log(type, state)
    })

    // 6. 点击删除按钮 发送ajax请求
    $('tbody').on('click', '.delete', function () {
      let id = $(this).attr('dataid')
      // console.log(id)
      $.ajax({
        type: 'post',
        url: BigNew.article_delete,
        data: {
          id: id
        },
        success: function (backData) {
          // console.log(backData)
          if (backData.code == 204) {
            $('.modal-body p').text(backData.msg)
            $('#myModal').modal()
            $('#myModal').on('hidden.bs.modal', function () {
              // 6.3 判断页数需不需要改变
              // 最后一页最后一条时,需要前进一页
              if ($('tbody tr').length > 1 || selPage == 1) {
                list(selPage)
              } else {
                list(--selPage)
              }
            })
          }
        }
      })
    })

    // 7. 点击发表文章，触发二级菜单点击事件
    $('#release_btn').on('click', function () {
      parent.window
        .$('.level02 li')
        .eq(1)
        .click()
    })
    // 3. 自调用
    list()
  })
</script>
